import { quest } from "../../utils/http";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: [
      { id: 1, name: "小件", select: true, money: 3 },
      { id: 2, name: "中件", select: false, money: 5 },
      { id: 3, name: "大件", select: false, money: 7 },
    ],
    delivery: [],
    currentDelivery: [],
    detail: [],
    gender: [
      { id: 0, gender: "女", select: false },
      { id: 1, gender: "男", select: false },
      { id: 2, gender: "不限", select: false },
    ],
    myGender: { id: 2, gender: "不限", select: false },
    money: "",
    page: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let detail = JSON.parse(options.detail);
    this.setData({
      detail,
    });
    if (options.page == "跑腿") {
      this.setData({
        page: options.page,
        type: [],
      });
      return;
    }
    // 获取之前的快递点

    let money;
    let { type } = this.data;
    type.forEach((element) => {
      if (element.id == detail.type) {
        element.select = true;
        money = element.money;
      } else {
        element.select = false;
      }
    });

    let delivery = await quest({
      url: "delivery/list/byUser",
    });
    let currentDelivery;
    if (delivery.code == 200) {
      delivery.data.items.forEach((element) => {
        if (element.id == detail.deliveryId) {
          currentDelivery = element;
        }
      });
      this.setData({
        delivery: delivery.data.items,
        currentDelivery,
        type,
        money,
        page: options.page,
      });
    }
  },
  remarkChange(e) {
    // console.log(e.detail.value);
    let detail = this.data.detail;
    detail.remark = e.detail.value;
    this.setData({ detail });
  },
  bindPickerChange(e) {
    // console.log(e.detail.value);
    let delivery = this.data.delivery[e.detail.value];
    this.setData({
      currentDelivery: delivery,
    });
  },
  recAddrChange(e) {
    let detail = this.data.detail;
    detail.recAddr = e.detail.value;
    this.setData({ detail });
  },
  bindGenderChange(e) {
    // console.log(e.detail.value);
    let myGender = this.data.gender[e.detail.value];
    this.setData({
      myGender,
    });
  },
  selectType(e) {
    let { id } = e.currentTarget.dataset;
    let { type, money } = this.data;
    type.forEach((element) => {
      if (element.id == id) {
        element.select = true;
        money = element.money;
      } else {
        element.select = false;
      }
    });
    this.setData({
      type,
      money,
    });
  },
  async modifyUpdate() {
    let { detail, type, currentDelivery, myGender, money } = this.data;
    detail.deliveryId = currentDelivery.id;
    detail.requireGender = myGender.id;
    detail.price = money;
    type.forEach((element) => {
      if (element.select == true) {
        detail.type = element.id;
      }
    });
    let data = await quest({
      url: "express/update",
      method: "post",
      data: {
        ...detail,
      },
    });
    console.log(data);
    if (data.code == 200) {
      wx.showModal({
        title: "提示",
        content: "更新成功",
        success(res) {
          if (res.confirm) {
          } else if (res.cancel) {
          }
        },
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
